/*
 * Created by SharpDevelop.
 * User: michaelo
 * Date: 18/05/2005
 * Time: 19:49
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using Orz.LogManager.Config;

namespace Orz.LogManager
{
	/// <summary>
	/// Description of MatchForm.
	/// </summary>
	public class MatchForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.CheckBox chkDontPrint;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.ComboBox cboColours;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.TextBox txtMatch;
		private System.Windows.Forms.CheckBox chkReplace;
		private System.Windows.Forms.ComboBox cboSearchMode;
		private System.Windows.Forms.CheckBox chkUnescapeQuerystring;
		private System.Windows.Forms.Button cmdCancel;
		private System.Windows.Forms.TextBox txtReplacement;
		private System.Windows.Forms.Button cmdOK;
		private System.Windows.Forms.CheckBox chkColour;
		
		
		private logsetLogfileMatch original_i;
		private logsetLogfileMatch working_i;
		private bool bWasCancelled_i;
		
		private HelpForm help_i;
		
		private const int CBO_INDEX_SEARCH_TYPE_MATCH = 0;
		private const int CBO_INDEX_SEARCH_TYPE_NOMATCH = 1;
		
		
		public MatchForm(logsetLogfileMatch match_p)
		{
			InitializeComponent();
			
			original_i = match_p;
			
			// Make a backup of the working set before we start
			working_i = LogsetLoader.CloneLogfileMatch (original_i);
			
			help_i = new HelpForm (this);
			
			PopulateFields();
		}
		
		
		
		public bool WasCancelled
		{
			get { return bWasCancelled_i; }
		}
		
		
		
		
		
		private void PopulateFields ()
		{
			if (null == working_i.pattern)
			{
				working_i.pattern = new logsetLogfileMatchPattern();
			}
			
			cboSearchMode.SelectedIndex = working_i.pattern.negated ? CBO_INDEX_SEARCH_TYPE_NOMATCH : CBO_INDEX_SEARCH_TYPE_MATCH;
			txtMatch.Text = working_i.pattern.Value;
			
			cboColours.Text = working_i.colour;
			txtReplacement.Text = working_i.replacement;
			
			chkColour.Checked = (null != working_i.colour);
			chkReplace.Checked = (null != working_i.replacement);
			
			chkUnescapeQuerystring.Checked = working_i.unescapeQuerystring;
			
			chkBeep.Checked = working_i.beep;
			
			chkDontPrint.Checked = working_i.dontPrint;
			
			UpdateButtons();
		}
		
		private void UpdateButtons()
		{
			if (chkDontPrint.Checked)
			{
				chkColour.Checked = false;
				chkReplace.Checked = false;
				chkUnescapeQuerystring.Checked = false;
			}
			
			chkColour.Enabled = !chkDontPrint.Checked;
			chkReplace.Enabled = !chkDontPrint.Checked;
			chkUnescapeQuerystring.Enabled = !chkDontPrint.Checked;
			chkBeep.Enabled = !chkDontPrint.Checked;
			
			cboColours.Enabled = chkColour.Checked  &&  !chkDontPrint.Checked;
			txtReplacement.Enabled = chkReplace.Checked  &&  !chkDontPrint.Checked;
		}
		
		
		private bool DoSave()
		{
			// Make sure a pattern has been defined
			if (0 == txtMatch.Text.Length)
			{
				MessageBox.Show ("You must at least specify a pattern.", this.Text);
				return false;
			}
			
			// Make sure they've specified a colour string if the checkbox is set
			if (chkColour.Checked  &&  0 == cboColours.Text.Length)
			{
				MessageBox.Show ("You should enter the name of a colour or uncheck the 'colour' checkbox.", this.Text);
				return false;
			}
			
			if (!chkColour.Checked  &&  !chkReplace.Checked  &&  !chkDontPrint.Checked  &&  !chkUnescapeQuerystring.Checked  &&  !chkBeep.Checked)
			{
				MessageBox.Show ("What's the point of searching for something if you don't want to do something with it!?", this.Text);
				return false;
			}
			
			
			// Copy the values across to the working version
			if (original_i.pattern == null)
			{
				original_i.pattern = new logsetLogfileMatchPattern();
			}
			
			original_i.pattern.Value = txtMatch.Text;
			original_i.pattern.negated = (CBO_INDEX_SEARCH_TYPE_NOMATCH == cboSearchMode.SelectedIndex ? true : false);
			
			original_i.colour = (chkColour.Checked ? cboColours.Text : null);
			original_i.replacement = (chkReplace.Checked ? txtReplacement.Text : null);
			original_i.unescapeQuerystring = (chkUnescapeQuerystring.Checked);
			original_i.beep = (chkBeep.Checked);
			original_i.dontPrint = (chkDontPrint.Checked);
			
			return true;
		}
		
		
		
		
		
		
		
		
		
		
		
		void CmdOKClick(object sender, System.EventArgs e)
		{
			if (DoSave())
			{
				bWasCancelled_i = false;
				this.Close();
			}
		}
		
		void CmdCancelClick(object sender, System.EventArgs e)
		{
			bWasCancelled_i = true;
			this.Close();
		}
		
		void ChkColourCheckedChanged(object sender, System.EventArgs e)
		{
			UpdateButtons();
			if (! chkColour.Checked) cboColours.Text = "";
		}
		
		void ChkReplaceCheckedChanged(object sender, System.EventArgs e)
		{
			UpdateButtons();
			if (! chkReplace.Checked) txtReplacement.Text = "";
		}
		
		void CboColoursSelectedIndexChanged(object sender, System.EventArgs e)
		{
			
		}
		
		void TxtReplacementTextChanged(object sender, System.EventArgs e)
		{
			
		}
		
		
		
		
		
		
		
		
		
		
		
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			this.chkColour = new System.Windows.Forms.CheckBox();
			this.cmdOK = new System.Windows.Forms.Button();
			this.txtReplacement = new System.Windows.Forms.TextBox();
			this.cmdCancel = new System.Windows.Forms.Button();
			this.chkUnescapeQuerystring = new System.Windows.Forms.CheckBox();
			this.cboSearchMode = new System.Windows.Forms.ComboBox();
			this.chkReplace = new System.Windows.Forms.CheckBox();
			this.txtMatch = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.cboColours = new System.Windows.Forms.ComboBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.chkDontPrint = new System.Windows.Forms.CheckBox();
			this.chkBeep = new System.Windows.Forms.CheckBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// chkColour
			// 
			this.chkColour.Location = new System.Drawing.Point(16, 32);
			this.chkColour.Name = "chkColour";
			this.chkColour.Size = new System.Drawing.Size(184, 23);
			this.chkColour.TabIndex = 1;
			this.chkColour.Text = "Colour them in!";
			this.chkColour.CheckedChanged += new System.EventHandler(this.ChkColourCheckedChanged);
			// 
			// cmdOK
			// 
			this.cmdOK.Location = new System.Drawing.Point(440, 16);
			this.cmdOK.Name = "cmdOK";
			this.cmdOK.Size = new System.Drawing.Size(75, 24);
			this.cmdOK.TabIndex = 7;
			this.cmdOK.Tag = "Closes this dialog box and temporarily saves the changes you have made.";
			this.cmdOK.Text = "&OK";
			this.cmdOK.Click += new System.EventHandler(this.CmdOKClick);
			// 
			// txtReplacement
			// 
			this.txtReplacement.Location = new System.Drawing.Point(216, 64);
			this.txtReplacement.Name = "txtReplacement";
			this.txtReplacement.Size = new System.Drawing.Size(192, 20);
			this.txtReplacement.TabIndex = 4;
			this.txtReplacement.Tag = "Any lines which match the regular expression above will have that pattern replace" +
			"d with whatever you put in here.  You can use back references like $1 to refer t" +
			"o anything in brackets in your pattern.";
			this.txtReplacement.TextChanged += new System.EventHandler(this.TxtReplacementTextChanged);
			// 
			// cmdCancel
			// 
			this.cmdCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cmdCancel.Location = new System.Drawing.Point(440, 48);
			this.cmdCancel.Name = "cmdCancel";
			this.cmdCancel.Size = new System.Drawing.Size(75, 24);
			this.cmdCancel.TabIndex = 8;
			this.cmdCancel.Tag = "Closes this dialog box, undoing any changes you have made.";
			this.cmdCancel.Text = "&Cancel";
			this.cmdCancel.Click += new System.EventHandler(this.CmdCancelClick);
			// 
			// chkUnescapeQuerystring
			// 
			this.chkUnescapeQuerystring.Location = new System.Drawing.Point(16, 96);
			this.chkUnescapeQuerystring.Name = "chkUnescapeQuerystring";
			this.chkUnescapeQuerystring.Size = new System.Drawing.Size(232, 22);
			this.chkUnescapeQuerystring.TabIndex = 3;
			this.chkUnescapeQuerystring.Text = "Unescape querystring parameters";
			this.chkUnescapeQuerystring.CheckedChanged += new System.EventHandler(this.ChkReplaceCheckedChanged);
			// 
			// cboSearchMode
			// 
			this.cboSearchMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboSearchMode.Items.AddRange(new object[] {
									"Lines containing (regex):",
									"Lines NOT containing (regex):"});
			this.cboSearchMode.Location = new System.Drawing.Point(16, 24);
			this.cboSearchMode.Name = "cboSearchMode";
			this.cboSearchMode.Size = new System.Drawing.Size(192, 21);
			this.cboSearchMode.TabIndex = 6;
			// 
			// chkReplace
			// 
			this.chkReplace.Location = new System.Drawing.Point(16, 64);
			this.chkReplace.Name = "chkReplace";
			this.chkReplace.Size = new System.Drawing.Size(200, 22);
			this.chkReplace.TabIndex = 3;
			this.chkReplace.Text = "Replace the matching text with:";
			this.chkReplace.CheckedChanged += new System.EventHandler(this.ChkReplaceCheckedChanged);
			// 
			// txtMatch
			// 
			this.txtMatch.Location = new System.Drawing.Point(216, 24);
			this.txtMatch.Name = "txtMatch";
			this.txtMatch.Size = new System.Drawing.Size(192, 20);
			this.txtMatch.TabIndex = 0;
			this.txtMatch.Tag = "Any lines in the log file matching this text or regular expression will be replac" +
			"ed with the text below, or coloured in, or both.";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.txtMatch);
			this.groupBox1.Controls.Add(this.cboSearchMode);
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(424, 64);
			this.groupBox1.TabIndex = 4;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Search criteria";
			// 
			// cboColours
			// 
			this.cboColours.Items.AddRange(new object[] {
									"Red",
									"Orange",
									"Yellow",
									"Green",
									"Cyan",
									"Blue",
									"Purple",
									"Pink",
									"Moccasin",
									"LightYellow",
									"LightGreen",
									"LightCyan",
									"LightBlue",
									"Plum"});
			this.cboColours.Location = new System.Drawing.Point(216, 32);
			this.cboColours.Name = "cboColours";
			this.cboColours.Size = new System.Drawing.Size(192, 21);
			this.cboColours.TabIndex = 2;
			this.cboColours.Tag = "A colour to highlight any lines which match the text or pattern above.";
			this.cboColours.Text = "Red";
			this.cboColours.SelectedIndexChanged += new System.EventHandler(this.CboColoursSelectedIndexChanged);
			this.cboColours.SelectionChangeCommitted += new System.EventHandler(this.CboColoursSelectedIndexChanged);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.chkBeep);
			this.groupBox2.Controls.Add(this.chkDontPrint);
			this.groupBox2.Controls.Add(this.chkColour);
			this.groupBox2.Controls.Add(this.chkReplace);
			this.groupBox2.Controls.Add(this.txtReplacement);
			this.groupBox2.Controls.Add(this.cboColours);
			this.groupBox2.Controls.Add(this.chkUnescapeQuerystring);
			this.groupBox2.Location = new System.Drawing.Point(8, 80);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(424, 232);
			this.groupBox2.TabIndex = 7;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Action to take on matching lines";
			// 
			// chkDontPrint
			// 
			this.chkDontPrint.Location = new System.Drawing.Point(16, 163);
			this.chkDontPrint.Name = "chkDontPrint";
			this.chkDontPrint.Size = new System.Drawing.Size(184, 22);
			this.chkDontPrint.TabIndex = 5;
			this.chkDontPrint.Text = "Don\'t print the lines";
			this.chkDontPrint.CheckedChanged += new System.EventHandler(this.ChkReplaceCheckedChanged);
			// 
			// chkBeep
			// 
			this.chkBeep.Location = new System.Drawing.Point(16, 130);
			this.chkBeep.Name = "chkBeep";
			this.chkBeep.Size = new System.Drawing.Size(184, 22);
			this.chkBeep.TabIndex = 5;
			this.chkBeep.Text = "Play a beep";
			this.chkBeep.CheckedChanged += new System.EventHandler(this.ChkReplaceCheckedChanged);
			// 
			// MatchForm
			// 
			this.AcceptButton = this.cmdOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.cmdCancel;
			this.ClientSize = new System.Drawing.Size(522, 324);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.cmdOK);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.cmdCancel);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "MatchForm";
			this.ShowInTaskbar = false;
			this.Text = "Search / replace / colour";
			this.groupBox1.ResumeLayout(false);
			this.groupBox1.PerformLayout();
			this.groupBox2.ResumeLayout(false);
			this.groupBox2.PerformLayout();
			this.ResumeLayout(false);
		}
		private System.Windows.Forms.CheckBox chkBeep;
		#endregion
		
		
	}
}
